package gw.com.android.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.gwtsz.chart.utils.Utils;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.ui.views.CommonTitleBar;
import gw.com.android.utils.MobclickEventUtlis;
import www.com.library.model.DataItemDetail;

/**
 * 帮助 弹窗-联系客服-常见问题
 * @author jett
 * @since 2017-07-27.
 */
public class AssistantDialog {

    public static final int REGISTER = 0;
    public static final int DEPOSIT = 1;
    public static final int TRADE = 2;

    private FragmentActivity activity;
    private View anchorView;
    private Dialog dialog;
    private int from;

    public void attach(FragmentActivity activity, CommonTitleBar titleBar, int from) {
        this.activity = activity;
        this.anchorView = titleBar.mRightText;
        this.from = from;
        titleBar.setRightText(R.string.customer_title_help);
        this.anchorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
    }

    @SuppressLint("RtlHardcoded")
    private void initDialog() {
        if (dialog != null) {
            return;
        }
        View root = View.inflate(activity, R.layout.dialog_assistant, null);
        initView(root);

        ViewGroup.LayoutParams contentParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog = new Dialog(activity, R.style.dialog_no_title);
        //dialog.addContentView(root, params);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(root, contentParam);

        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            Utils.init(activity);
            params.gravity = Gravity.RIGHT | Gravity.TOP;
            params.x = (int) Utils.convertDpToPixel(2);
            params.y = (int) (anchorView.getY() + anchorView.getHeight() + Utils.convertDpToPixel(8));
            params.width = (int) Utils.convertDpToPixel(135);
            params.height = (int) Utils.convertDpToPixel(97F);
            window.setAttributes(params);
            window.setWindowAnimations(R.style.dialog_fade_translate);
        }
    }

    private void toggle() {
        initDialog();
        if (dialog.isShowing()) {
            dialog.dismiss();
        } else {
            dialog.show();
        }
    }

    private void initView(View dialogView) {
        AssistantListener listener = new AssistantListener();
        dialogView.findViewById(R.id.ll_assistant_customer_service).setOnClickListener(listener);
        dialogView.findViewById(R.id.ll_assistant_faq).setOnClickListener(listener);
    }

    private class AssistantListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_assistant_customer_service:
                    MobclickEventUtlis.MobclickEventByAccountType(activity, "service");
                    ConfigSettingDeal mDeal = new ConfigSettingDeal();
                    if (ConfigUtil.instance().customerFunction() == 2) {
                        mDeal.openContact(activity, ConfigType.QUOTE_CONTACT_TAG);
                    } else {
                        mDeal.openQQ(activity, getQQFrom());
                    }
                    break;
                case R.id.ll_assistant_faq:
                    MobclickEventUtlis.MobclickEventByAccountType(activity, "faq");
                    DataItemDetail item = new DataItemDetail();
                    item.setStringValue(ConfigType.CONFIG_TYPE_KEY_TAG, ConfigType.FAQ_TAG);
                    //item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, getFaqUrl());
                    item.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, AppMain.getAppString(R.string.customer_faq));
                    ActivityManager.showWebPageActivity(activity, item, AppMain.getAppString(R.string.btn_back));
                    break;
                default:
                    break;
            }
            toggle();
        }
    }

    private String getQQFrom() {
        switch (from) {
            case REGISTER:
                return ConfigType.QQ_SOURCE_REGISTER;
            case DEPOSIT:
                return ConfigType.QQ_SOURCE_DEPOSIT;
            case TRADE:
                return ConfigType.QQ_SOURCE_ORDER;
            default:
                return null;
        }
    }

    private String getFaqUrl() {
        String url = ConfigUtil.instance().getUrlPath("faqUrl");
        if (!TextUtils.isEmpty(url)) {
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            if (url.endsWith("register")) {
                url = url.substring(0, url.length() - "register".length());
            }
            if (url.endsWith("deposit")) {
                url = url.substring(0, url.length() - "deposit".length());
            }
            if (url.endsWith("trade")) {
                url = url.substring(0, url.length() - "trade".length());
            }
        }
        switch (from) {
            case REGISTER:
                return url + "register/";
            case DEPOSIT:
                return url + "deposit/";
            case TRADE:
                return url + "trade/";
            default:
                return url;
        }
    }

}
